package com.naizhuai.ui.actionmodule;

import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.R;
import com.naizhuai.network.exception.ApiException;
import com.naizhuai.network.exception.ServerException;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.actionmodule.presenter.ActionDataApiStore;
import com.naizhuai.ui.actionmodule.presenter.ActionDataApiStoreIml;
import com.naizhuai.ui.adapter.ListSlideLabelsHolderAdapter;
import com.naizhuai.ui.adapter.ManageDividerItemDecoration;
import com.naizhuai.ui.components.SlidingUpPanelLayout;
import com.naizhuai.ui.entity.management.CompanyBean;
import com.naizhuai.ui.entity.mainbusiness.SlideContentBean;
import com.naizhuai.util.ConstantsPersonResources;
import com.naizhuai.util.DialogUtils;
import com.naizhuai.util.SharedPreferenceUtil;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import java.util.List;
import rx.Observer;


public class ActionModuleMainActivity extends BaseActivity {
    private static final String TAG = "ActionMainActivity";

    private SlidingUpPanelLayout mLayout;
    private RecyclerView slideFieldRecycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        initRecyclerView();
        initBasicData();
        initView();
        requestData();
    }

    private void initBasicData() {
        SharedPreferenceUtil.getInstance(this).saveValue(CompanyBean.COMPANY_NAME_KEY, "东莞耐助智能有限公司");
        SharedPreferenceUtil.getInstance(this).saveValue( CompanyBean.COMPANY_ID_KEY, ConstantsPersonResources.COMPANY_ID_TEST_VALUE);
        SharedPreferenceUtil.getInstance(this).saveValue( CompanyBean.COMPANY_THUMB_URL_KEY, "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2887997545,417602002&fm=27&gp=0.jpg");
        SharedPreferenceUtil.getInstance(this).saveValue(ConstantsPersonResources.SESSION_ID_KEY, ConstantsPersonResources.SESSION_TEST_VALUE);
        SharedPreferenceUtil.getInstance(this).saveValue(PersonUserBean.USER_ID_KEY, ConstantsPersonResources.USER_ID_TEST_VALUE);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_action_main;
    }

    private void initView() {
        initSlide();
        slideFieldRecycler = (RecyclerView) findViewById(R.id.list);
        slideFieldRecycler.setLayoutManager(new LinearLayoutManager(this));
        slideFieldRecycler.addItemDecoration(new ManageDividerItemDecoration(this, ManageDividerItemDecoration.VERTICAL_LIST, 20, false));
//        slideFieldRecycler.setAdapter(new SimpleTestAdapter(this));
    }


    private void initSlide() {
        mLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
        mLayout.addPanelSlideListener(new SlidingUpPanelLayout.PanelSlideListener() {
            @Override
            public void onPanelSlide(View panel, float slideOffset) {
            }

            @Override
            public void onPanelStateChanged(View panel, SlidingUpPanelLayout.PanelState previousState, SlidingUpPanelLayout.PanelState newState) {
            }
        });
        mLayout.setFadeOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
            }
        });
    }


    private void requestData() {
        Observer<List<SlideContentBean>> observer = new Observer<List<SlideContentBean>>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                if(e instanceof ApiException){
                    ApiException server= (ApiException) e;
                    DialogUtils.showPromptDialog(ActionModuleMainActivity.this,server.getDisplayMessage());
                }
            }

            @Override
            public void onNext(List<SlideContentBean> result) {
                initRecyclerView(result);
            }
        };

        ActionDataApiStore action= ActionDataApiStoreIml.getInstance();
        action.getSlideContentList().subscribe(observer);
    }

    private void initRecyclerView(List<SlideContentBean> slideFieldList) {
        ListSlideLabelsHolderAdapter adapter = new ListSlideLabelsHolderAdapter(this, slideFieldList);
        slideFieldRecycler.setAdapter(adapter);
    }


    @Override
    public void onBackPressed() {
        if (mLayout != null &&
                (mLayout.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED || mLayout.getPanelState() == SlidingUpPanelLayout.PanelState.ANCHORED)) {
            mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
        } else {
            super.onBackPressed();
        }
    }
}
